package com.kestrel.space.core.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.kestrel.space.common.utils.Generator;
import com.kestrel.space.common.utils.LargePage;
import com.kestrel.space.core.mapper.FileMappingOrgMapper;
import com.kestrel.space.core.model.FileMappingOrg;
import com.kestrel.space.core.query.FileMappingOrgQuery;
import com.kestrel.space.core.service.FileMappingOrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author yebolin
 * @date 2024/9/6
 */
@Service
public class FileMappingOrgServiceImpl implements FileMappingOrgService {
    @Autowired
    private FileMappingOrgMapper fileMappingOrgMapper;

    @Override
    public List<FileMappingOrg> listContent(FileMappingOrgQuery query, LargePage page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNum(), page.getPageSize());
        }
        FileMappingOrg fileMappingOrg = BeanUtil.copyProperties(query, FileMappingOrg.class);
        return fileMappingOrgMapper.select(fileMappingOrg);
    }

    @Override
    public String saveOrUpdate(FileMappingOrg fileMappingOrg) {
        String message = "插入成功";
        if (fileMappingOrg.getId() == null) {
            fileMappingOrg.setId(Generator.generateId());
            fileMappingOrgMapper.insert(fileMappingOrg);
        } else {
            message = "更新成功";
            fileMappingOrgMapper.updateByPrimaryKeySelective(fileMappingOrg);
        }
        return message;
    }

    @Override
    public String batchSaveOrUpdate(List<FileMappingOrg> fileMappingOrgList) {
        String message = "批量插入成功";
        if (CollectionUtils.isEmpty(fileMappingOrgList)) {
            return "集合为空";
        }
        if(fileMappingOrgList.get(0).getId() == null){
            for (FileMappingOrg fileMappingOrg : fileMappingOrgList) {
                fileMappingOrg.setId(Generator.generateId());
                fileMappingOrgMapper.insert(fileMappingOrg);
            }
        } else {
            message = "批量更新成功";
            for (FileMappingOrg fileMappingOrg : fileMappingOrgList) {
                fileMappingOrg.setId(Generator.generateId());
                fileMappingOrgMapper.updateByPrimaryKey(fileMappingOrg);
            }
        }
        return message;
    }
}
